package cs342.uwosh.group1.mycontacts;

import com.mycontacts.R;
import com.mycontacts.R.id;
import com.mycontacts.R.layout;
import com.mycontacts.R.menu;

import cs342.uwosh.group1.mycontacts.Threads.AddContactTask;
import cs342.uwosh.group1.mycontacts.Threads.UpdateContactTask;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddContact extends Activity {

	String id = "";
	Contact currentContact;
	int state = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_contact);
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			Button addB = (Button)findViewById(R.id.addButton);
			addB.setText("Update!");
			
			Intent intent = getIntent();
			id = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
			
			currentContact = MainActivity.allContacts.get(id);
			
			EditText na = (EditText)findViewById(R.id.nameText);
			na.setText(currentContact.getName());
			
			EditText ad = (EditText)findViewById(R.id.addrText);
			ad.setText(currentContact.getAddress());
			
			EditText ce = (EditText)findViewById(R.id.cellText);
			ce.setText(currentContact.getCellNumber());
			
			EditText alt = (EditText)findViewById(R.id.altText);
			alt.setText(currentContact.getAltNumber());
			
			EditText em = (EditText)findViewById(R.id.emailText);
			em.setText(currentContact.getEmail());
			
			EditText com = (EditText)findViewById(R.id.comText);
			com.setText(currentContact.getComments());
			
			state = 1;
		}else{
			state = 0;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_add_contact, menu);
		return true;
	}
	
	public void createContact(View view){
		EditText n = (EditText)findViewById(R.id.nameText);
		EditText a = (EditText)findViewById(R.id.addrText);
		EditText c = (EditText)findViewById(R.id.cellText);
		EditText al = (EditText)findViewById(R.id.altText);
		EditText e = (EditText)findViewById(R.id.emailText);
		EditText co = (EditText)findViewById(R.id.comText);
		String name = n.getText().toString();
		String address = a.getText().toString();
		String cell = c.getText().toString();
		String alternate = al.getText().toString();
		String email = e.getText().toString();
		String comment = co.getText().toString();
		int category = MainActivity.categoryViewed;
		if(state == 0){
			Contact adder = new Contact(name, address, cell, alternate, email, comment, category);
			new AddContactTask().execute(adder);
		}else if(state == 1){
			currentContact.setName(name);
			currentContact.setAddress(address);
			currentContact.setCellNumber(cell);
			currentContact.setAltNumber(alternate);
			currentContact.setEmail(email);
			currentContact.setComments(comment);
			new UpdateContactTask().execute(currentContact);
		}
		//new GetAllContactTask().execute();
		MainActivity.changed = true;
		finish();
	}

}
